package com.ruix.app.widget.dialog.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.Html;
import android.text.SpannedString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.ruix.app.R;
import com.ruix.app.widget.dialog.core.BaseDialogBuilder;
import com.ruix.app.widget.dialog.core.BaseDialogFragment;
import com.ruix.app.widget.dialog.iface.IBuilderListener;
import com.ruix.app.widget.dialog.iface.IMessageListener;
import com.ruix.app.widget.dialog.iface.INegativeButtonDialogListener;
import com.ruix.app.widget.dialog.iface.INeutralButtonDialogListener;
import com.ruix.app.widget.dialog.iface.IPositiveButtonDialogListener;

import java.util.List;


/**
 * User: Ray
 * Date: 16/6/28
 * ReadMe: Simple－fragment
 */
public class SimpleDialogFragment extends BaseDialogFragment {

    protected final static String ARG_MESSAGE = "message";
    protected final static String ARG_TITLE = "title";
    protected final static String ARG_POSITIVE_BUTTON = "positive_button";
    protected final static String ARG_NEGATIVE_BUTTON = "negative_button";
    protected final static String ARG_NEUTRAL_BUTTON = "neutral_button";
    protected final static String ARG_NOT_DISMISS_WHEN_CLICK_POSITIVE = "not_dismiss_when_click_positive";
    protected final static String ARG_CUSTOM_VIEW_RES_ID = "custom_view_res_id";
    protected final static String ARG_NOTIFY_GET_NEW_MESSAGE = "notify_get_new_message";

    private static Context mContext;

    @Override
    public int getTheme() {
        return R.style.DialogTheme;
    }

    public static SimpleDialogBuilder createBuilder(FragmentActivity context) {
        mContext = context;
        return createBuilder(context, context.getSupportFragmentManager());
    }

    private static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) {
        return new SimpleDialogBuilder(context, fragmentManager, SimpleDialogFragment.class);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    /**
     * Key method for extending {@link com.ruix.app.widget.dialog.fragment.SimpleDialogFragment}.
     * Children can extend this to add more things to base builder.
     */
    @Override
    protected BaseDialogFragment.Builder build(final BaseDialogFragment.Builder builder) {
        final CharSequence title = getTitle();
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }

        final CharSequence message = getMessage();
        if (!TextUtils.isEmpty(message)) {
            builder.setMessage(message);
        }

        final CharSequence positiveButtonText = getPositiveButtonText();
        if (!TextUtils.isEmpty(positiveButtonText)) {
            builder.setPositiveButton(positiveButtonText, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final boolean notifyGetNewMessage = getNotifyGetNewMessage();
                    if (notifyGetNewMessage) {// 在全局页面显示消息对话框
                        for (IMessageListener listener : getMessageListeners()) {
                            listener.onGetNewMessage(mRequestCode);
                        }
                    } else {
                        for (IPositiveButtonDialogListener listener : getPositiveButtonDialogListeners()) {
                            listener.onPositiveButtonClicked(mRequestCode);
                        }
                    }

                    final boolean notDismissWhenClikcPositive = getNotDismissWhenClickPositive();
                    if (!notDismissWhenClikcPositive) {
                        dismiss();
                    }
                }
            });
        }

        final CharSequence negativeButtonText = getNegativeButtonText();
        if (!TextUtils.isEmpty(negativeButtonText)) {
            builder.setNegativeButton(negativeButtonText, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (INegativeButtonDialogListener listener : getNegativeButtonDialogListeners()) {
                        listener.onNegativeButtonClicked(mRequestCode);
                    }
                    dismiss();
                }
            });
        }

        final CharSequence neutralButtonText = getNeutralButtonText();
        if (!TextUtils.isEmpty(neutralButtonText)) {
            builder.setNeutralButton(neutralButtonText, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (INeutralButtonDialogListener listener : getNeutralButtonDialogListeners()) {
                        listener.onNeutralButtonClicked(mRequestCode);
                    }
                    dismiss();
                }
            });
        }

        final int customViewResId = getCustomViewResId();
        if (customViewResId != 0) {
            builder.setView(LayoutInflater.from(mContext).inflate(customViewResId, null));
        }


        // 构建完毕
        builder.setBuilderListener(new IBuilderListener() {
            @Override
            public void onBuildDown(int requestCode, Builder tempBuilder, DialogFragment fragment) {
                for (IBuilderListener listener : getBuilderListeners()) {
                    listener.onBuildDown(mRequestCode, builder, fragment);
                }
            }
        });


        return builder;
    }

    protected CharSequence getMessage() {
        return getArguments().getCharSequence(ARG_MESSAGE);
    }

    protected CharSequence getTitle() {
        return getArguments().getCharSequence(ARG_TITLE);
    }

    protected CharSequence getPositiveButtonText() {
        return getArguments().getCharSequence(ARG_POSITIVE_BUTTON);
    }

    protected CharSequence getNegativeButtonText() {
        return getArguments().getCharSequence(ARG_NEGATIVE_BUTTON);
    }

    protected CharSequence getNeutralButtonText() {
        return getArguments().getCharSequence(ARG_NEUTRAL_BUTTON);
    }

    protected boolean getNotDismissWhenClickPositive() {
        return getArguments().getBoolean(ARG_NOT_DISMISS_WHEN_CLICK_POSITIVE);
    }


    protected int getCustomViewResId() {
        return getArguments().getInt(ARG_CUSTOM_VIEW_RES_ID);
    }

    protected boolean getNotifyGetNewMessage() {
        return getArguments().getBoolean(ARG_NOTIFY_GET_NEW_MESSAGE);
    }


    /**
     * Get positive button dialog listeners.
     * There might be more than one listener.
     *
     * @return Dialog listeners
     * @since 2.1.0
     */
    protected List<IPositiveButtonDialogListener> getPositiveButtonDialogListeners() {
        return getDialogListeners(IPositiveButtonDialogListener.class);
    }

    /**
     * Get negative button dialog listeners.
     * There might be more than one listener.
     *
     * @return Dialog listeners
     * @since 2.1.0
     */
    protected List<INegativeButtonDialogListener> getNegativeButtonDialogListeners() {
        return getDialogListeners(INegativeButtonDialogListener.class);
    }

    /**
     * Get neutral button dialog listeners.
     * There might be more than one listener.
     *
     * @return Dialog listeners
     * @since 2.1.0
     */
    protected List<INeutralButtonDialogListener> getNeutralButtonDialogListeners() {
        return getDialogListeners(INeutralButtonDialogListener.class);
    }

    protected List<IBuilderListener> getBuilderListeners() {
        return getDialogListeners(IBuilderListener.class);
    }

    protected List<IMessageListener> getMessageListeners() {
        return getDialogListeners(IMessageListener.class);
    }


    public static class SimpleDialogBuilder extends BaseDialogBuilder<SimpleDialogBuilder> {

        private CharSequence mTitle;
        private CharSequence mMessage;
        private CharSequence mPositiveButtonText;
        private CharSequence mNegativeButtonText;
        private CharSequence mNeutralButtonText;
        private boolean mNotDismissWhenClickPositive;
        private int mCustomViewResId;
        private boolean mNotifyGetNewMessage;

        protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class<? extends SimpleDialogFragment> clazz) {
            super(context, fragmentManager, clazz);
        }

        @Override
        protected SimpleDialogBuilder self() {
            return this;
        }

        public SimpleDialogBuilder setTitle(int titleResourceId) {
            mTitle = mContext.getString(titleResourceId);
            return this;
        }


        public SimpleDialogBuilder setTitle(CharSequence title) {
            mTitle = title;
            return this;
        }

        public SimpleDialogBuilder setMessage(int messageResourceId) {
            mMessage = mContext.getText(messageResourceId);
            return this;
        }

        /**
         * Allow to set resource string with HTML formatting and bind %s,%i.
         * This is workaround for https://code.google.com/p/android/issues/detail?id=2923
         */
        public SimpleDialogBuilder setMessage(int resourceId, Object... formatArgs) {
            mMessage = Html.fromHtml(String.format(Html.toHtml(new SpannedString(mContext.getText(resourceId))), formatArgs));
            return this;
        }

        public SimpleDialogBuilder setMessage(CharSequence message) {
            mMessage = message;
            return this;
        }

        public SimpleDialogBuilder setPositiveButtonText(int textResourceId) {
            mPositiveButtonText = mContext.getString(textResourceId);
            return this;
        }

        public SimpleDialogBuilder setPositiveButtonText(CharSequence text) {
            mPositiveButtonText = text;
            return this;
        }

        public SimpleDialogBuilder setNegativeButtonText(int textResourceId) {
            mNegativeButtonText = mContext.getString(textResourceId);
            return this;
        }

        public SimpleDialogBuilder setNegativeButtonText(CharSequence text) {
            mNegativeButtonText = text;
            return this;
        }

        public SimpleDialogBuilder setNeutralButtonText(int textResourceId) {
            mNeutralButtonText = mContext.getString(textResourceId);
            return this;
        }

        public SimpleDialogBuilder setNeutralButtonText(CharSequence text) {
            mNeutralButtonText = text;
            return this;
        }

        public SimpleDialogBuilder setNotDismissWhenClickPositive(boolean notDismissWhenClickPositive) {
            mNotDismissWhenClickPositive = notDismissWhenClickPositive;
            return this;
        }

        public SimpleDialogBuilder setCustomView(int customViewResId) {
            mCustomViewResId = customViewResId;
            return this;
        }

        public SimpleDialogBuilder setNotifyGetNewMessage(boolean notifyGetNewMessage) {
            mNotifyGetNewMessage = notifyGetNewMessage;
            return this;
        }

        @Override
        protected Bundle prepareArguments() {
            Bundle args = new Bundle();
            args.putCharSequence(SimpleDialogFragment.ARG_MESSAGE, mMessage);
            args.putCharSequence(SimpleDialogFragment.ARG_TITLE, mTitle);
            args.putCharSequence(SimpleDialogFragment.ARG_POSITIVE_BUTTON, mPositiveButtonText);
            args.putCharSequence(SimpleDialogFragment.ARG_NEGATIVE_BUTTON, mNegativeButtonText);
            args.putCharSequence(SimpleDialogFragment.ARG_NEUTRAL_BUTTON, mNeutralButtonText);
            args.putBoolean(SimpleDialogFragment.ARG_NOT_DISMISS_WHEN_CLICK_POSITIVE, mNotDismissWhenClickPositive);
            args.putInt(SimpleDialogFragment.ARG_CUSTOM_VIEW_RES_ID, mCustomViewResId);
            args.putBoolean(SimpleDialogFragment.ARG_NOTIFY_GET_NEW_MESSAGE, mNotifyGetNewMessage);
            return args;
        }
    }
}
